<?php

class ScheduleController extends Controller {

    /**
     * Declares class-based actions.
     */
    private $scheduleService;

//    public function __construct() {
//        $this->scheduleService=new ScheduleService();
//    }
    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    /**
     * 按照排期一个维度获取列表
     */
    public function actionScheduleList() {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        //$schedule=array("dateline"=>"2014-11-11", "one"=> "健康体检", "two"=> "牙医宝", "three"=>"哎呀包", "four"=>"123", "five"=> "123");


        $scheduleService = new ScheduleService();
        //$scheduleList=array($schedule);
        $scheduleList = $scheduleService->scheduleList();

        echo json_encode($scheduleList);
        // $this->render('schedulelist',$scheduleList);
    }

    /**
     * 按照排期和项目两个维度排期
     */
    public function actionNewscheduleList() {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        //$schedule=array("dateline"=>"2014-11-11", "one"=> "健康体检", "two"=> "牙医宝", "three"=>"哎呀包", "four"=>"123", "five"=> "123");


        $scheduleService = new ScheduleService();
        //$scheduleList=array($schedule);
        $scheduleList = $scheduleService->newscheduleList();

        echo json_encode($scheduleList);
        // $this->render('schedulelist',$scheduleList);
    }

    /*
     * modifyschedule
     * 修改排期功能
     */

    public function actionModifyschedule() {

        $scheduleInfo = array();
//捕获到的排期信息，
        //缺少判断信息
        if (isset($_POST['sid'])) {
            $scheduleInfo['sid'] = $_POST['sid'];
        }
        if (isset($_POST['project'])) {
            $scheduleInfo['project'] = $_POST['project'];
        }
        if (isset($_POST['module'])) {
            $scheduleInfo['module'] = $_POST['module'];
        }
        if (isset($_POST['func'])) {
            $scheduleInfo['func'] = $_POST['func'];
        }
        if (isset($_POST['detail'])) {
            $scheduleInfo['detail'] = $_POST['detail'];
        }
        if (isset($_POST['people'])) {
            $scheduleInfo['people'] = $_POST['people'];
        }
        if (isset($_POST['dateline'])) {
            $scheduleInfo['dateline'] = $_POST['dateline'];
        }
        date_default_timezone_set("PRC"); //设置默认时区
        $scheduleInfo['addtime'] = date("Y-m-d H:i ", $timestamp = time());
        $scheduleInfo['adder'] = "刘鑫";
        // $scheduleInfo['sid'] = time();
        // echo json_encode($scheduleInfo);

        $scheduleService = new ScheduleService();
        $returnResult = $scheduleService->updateschedule($scheduleInfo);
        //  JSONHelper::echoJSON($returnResult);
        echo json_encode($returnResult);

        //  echo $scheduleInfo['addtime'];
//        if (isset($_POST['email'])) {
//            $scheduleInfo['email'] = $_POST['email'];
//        }
        //$this->render('addschedule');
    }

    /**
     * 添加排期功能
     */
    public function actionAddschedule() {
        // renders the view file 'protected/views/site/index.php'
        // "project": project,
//                    "module": module,
//                    "func": func,
//                    "detail": detail,
//                    "people": people,
//                    "time": time 
        // using the default layout 'protected/views/layouts/main.php'
        $scheduleInfo = array();
//捕获到的排期信息，
        //缺少判断信息

        if (isset($_POST['project'])) {
            $scheduleInfo['project'] = $_POST['project'];
        }
        if (isset($_POST['module'])) {
            $scheduleInfo['module'] = $_POST['module'];
        }
        if (isset($_POST['func'])) {
            $scheduleInfo['func'] = $_POST['func'];
        }
        if (isset($_POST['detail'])) {
            $scheduleInfo['detail'] = $_POST['detail'];
        }
        if (isset($_POST['people'])) {
            $scheduleInfo['people'] = $_POST['people'];
        }
        if (isset($_POST['dateline'])) {
            $scheduleInfo['dateline'] = $_POST['dateline'];
        }
        date_default_timezone_set("PRC"); //设置默认时区
        $scheduleInfo['addtime'] = date("Y-m-d H:i ", $timestamp = time());
        $scheduleInfo['adder'] = "刘鑫";
        $scheduleInfo['sid'] = time();
        // echo json_encode($scheduleInfo);

        $scheduleService = new ScheduleService();
        $returnResult = $scheduleService->addschedule($scheduleInfo);
        JSONHelper::echoJSON($returnResult);


        //  echo $scheduleInfo['addtime'];
//        if (isset($_POST['email'])) {
//            $scheduleInfo['email'] = $_POST['email'];
//        }
        //$this->render('addschedule');
    }

    /**
     * 获得分页选择的html
     */
    public function actionGetFpage() {
        $page = $_POST["page"];
        if (preg_match('/\D/', $page)) {
            echo JSONHelper::ERROR_ILLEGAL_REQUEST;
        } else {
            $page = int($_POST["page"]);
            $pageservice = new ScheduleService();
            $html = $pageservice->getFpage($page);
            echo $html;
        }
    }

    /**
     * 获得分页内容
     */
    public function actionGetPage() {
        if (isset($_POST["page"])) {
            $page = $_POST["page"];
            if (preg_match('/\D/', $page)) {
                echo JSONHelper::ERROR_ILLEGAL_REQUEST;
            } else {
                $page = (int)$page;
                $pageservice = new ScheduleService();
                $result = $pageservice->getPage($page);
                echo json_encode($result);
            }
        }
    }

    /**
     * 获得项目列表
     */
    public function actionGetProject() {
        $schservice = new ScheduleService();
        $prolist = $schservice->getProject();
        echo json_encode($prolist);
    }

    /**
     * 获得项目和排期
     */
    public function actionGetProjectAndDateline() {
        $schservice = new ScheduleService();
        $projectlist = $schservice->getProject();
        $datelinelist = $schservice->getDateline();
        $result = array("projectlist" => $projectlist, "datelinelist" => $datelinelist);
        echo json_encode($result);
    }

    /**
     * 排期详情内容
     */
    public function actionScheduleDetails() {

        $this->render('scheduleDetails');
    }

    /**
     * 排期详情内容
     */
    public function actionGetAllScheduleDetials() {

        $scheduleService = new ScheduleService();
        $scheduleList = $scheduleService->getAllScheduleDetials();

        echo json_encode($scheduleList);
    }

    /**
     * 添加排期功能
     */
    public function actionProManagement() {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('proManagement');
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex() {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('index');
    }

    public function actionTest() {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('test');
    }

    public function actionTest2() {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('test2');
    }

    public function actionTest3() {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('test3');
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    /**
     * Displays the contact page
     */
    public function actionContact() {
        $model = new ContactForm;
        $name = $_GET['name'];

        if (isset($_POST['ContactForm'])) {
            $model->attributes = $_POST['ContactForm'];
            if ($model->validate()) {
                $name = '=?UTF-8?B?' . base64_encode($model->name) . '?=';
                $subject = '=?UTF-8?B?' . base64_encode($model->subject) . '?=';
                $headers = "From: $name <{$model->email}>\r\n" .
                        "Reply-To: {$model->email}\r\n" .
                        "MIME-Version: 1.0\r\n" .
                        "Content-Type: text/plain; charset=UTF-8";

                mail(Yii::app()->params['adminEmail'], $subject, $model->body, $headers);
                Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
                $this->refresh();
            }
        }

        //$this->render('contact',array('model'=>$model));
        $this->render('contact', array('model' => $model, 'name' => $name));
    }

    /**
     * Displays the login page
     */
    public function actionLogin() {
        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

    /**
     * 测试
     */
    public function actionTest1() {

        $model = new TestForm;
        if (isset($_POST['ContactForm'])) {
            $model->attributes = $_POST['ContactForm'];
            if ($model->validate()) {
                $name = '=?UTF-8?B?' . base64_encode($model->name) . '?=';
                $subject = '=?UTF-8?B?' . base64_encode($model->subject) . '?=';
                $headers = "From: $name <{$model->email}>\r\n" .
                        "Reply-To: {$model->email}\r\n" .
                        "MIME-Version: 1.0\r\n" .
                        "Content-Type: text/plain; charset=UTF-8";

                mail(Yii::app()->params['adminEmail'], $subject, $model->body, $headers);
                Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
                $this->refresh();
            }
        }
        $this->render('test', array('model' => $model));
    }

}
